﻿using System;
using System.ServiceProcess;
using FutianCloudAPIService.Utils;
using glTech.Log4netWrapper;

namespace FutianCloudAPIService
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            Logger.Initialize(PathUtil.GetLog4netPath());
            Logger.LogError("start");


            //Debug Code
            if (!Environment.UserInteractive)
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                new FutianCloudAPIService()
                };
                ServiceBase.Run(ServicesToRun);
            }
            else
            {
                FutianCloudAPIService service = new FutianCloudAPIService();
                service.Start();
                System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);  // forces debug to keep VS running while we debug the service  
            }

            Logger.LogInfo("process end");
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e != null && e.ExceptionObject != null)
            {
                Logger.LogError("未捕获异常");
                Logger.LogError4Exception((Exception)e.ExceptionObject);
            }
        }
    }
}
